package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.*;
import com.indusfo.spc.pojo.*;
import com.indusfo.spc.service.OrderFlowService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Author liub
 * @Date 2019/11/26 16:57
 * @Version 1.0
 */

@Service
public class OrderFlowServiceImpl implements OrderFlowService {


    /**
     * 声明日志的静态变量
     */
    private static final Logger logger = LoggerFactory.getLogger(OrderFlowServiceImpl.class);

    @Resource
    private OrderFlowMapper orderFlowMapper;

    @Resource
    private OrderStationMapper orderStationMapper;

    @Resource
    private OrderDeteMapper orderDeteMapper;

    @Resource
    private OrderBomMapper orderBomMapper;

    @Resource
    private OrderBadphenoMapper orderBadphenoMapper;

    @Resource
    private OrderRepairMapper orderRepairMapper;

    @Resource
    private OrderRepairFlowMapper orderRepairFlowMapper;

    @Resource
    private OrderRepairDeteMapper orderRepairDeteMapper;

    @Resource
    private MatFlowMapper matFlowMapper;

    @Resource
    private MatStationMapper matStationMapper;

    @Resource
    private MatDeteMapper matDeteMapper;

    @Resource
    private MatBomMapper matBomMapper;

    @Resource
    private MatBadphenoMapper matBadphenoMapper;

    @Resource
    private MatRepairMapper matRepairMapper;

    @Resource
    private MatRepairFlowMapper matRepairFlowMapper;

    @Resource
    private MatRepairDeteMapper matRepairDeteMapper;
    @Resource
    private RepairDetailMapper repairDetailMapper;


	@Resource
	private OrderTechMapper orderTechMapper;

	@Resource
	private OrderMapper orderMapper;

    @Resource
    private BatchMapper batchMapper;
	/**
     * 查询
     */
    @Override
    public JSONObject queryOrderFlow(OrderFlow orderFlow) {
        try {
            if (orderFlow.getOrderTechId() == null) {
                throw new ModifyFailedException(HintMessage.ORDERTECHID_NULL);
            }

            //获取页面的数据个数
            Integer pagesize = orderFlow.getPagesize();
            //获取查询的页数
            Integer pageindex = orderFlow.getPageindex();
            if (pagesize != null && pageindex != null) {
                orderFlow.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<OrderFlow> orderFlowList =new ArrayList<>();
            Integer count=0;
            if(orderFlow.getBatchId()==null){
                orderFlowList= orderFlowMapper.queryOrderFlow(orderFlow);
                count = orderFlowMapper.countOrderFlow(orderFlow);
            }else{
                orderFlowList= orderFlowMapper.queryOrderFlowByBatch(orderFlow);
                count = orderFlowMapper.countOrderFlowByBatch(orderFlow);
            }


            //如果页数和页面数量都不为空的情况下进行计数
            if (orderFlowList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, orderFlowList, 0);
            } else {
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, orderFlowList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    /**
     * 新增
     */
    @Override
    public JSONObject insertOrderFlow(OrderFlow orderFlow) {
        try {
            if (orderFlow.getOrderFlowId() != null) {
                throw new ModifyFailedException(HintMessage.INSERT_ID);
            }
            //初始化基础数据
            if (orderFlow.getDataState() == null) {
                orderFlow.setDataState(1);
            }
            //判重
            checkParam(orderFlow);
            //执行新增操作
            Integer row = orderFlowMapper.insertOrderFlow(orderFlow);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.INSERT_DEFEATED);
            }
            return JSONObject.oK(HintMessage.INSERT_SUCCESS);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }

    }

    /**
     * 修改
     */
    @Override
    public JSONObject updateOrderFlow(OrderFlow orderFlow) {
        try {
            if (orderFlow.getOrderFlowId() == null) {
                throw new ParamsErrorException(HintMessage.UPDATE_ID);
            }
            //初始化基础数据
            if (orderFlow.getDataState() == null) {
                orderFlow.setDataState(1);
            }
            //判重
            checkParam(orderFlow);
            //执行修改操作
            int row = orderFlowMapper.updateOrderFlow(orderFlow);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
            }
            return JSONObject.oK(HintMessage.UPDATE_SUCCESS);
        } catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 删除
     */
    @Override
    public JSONObject deleteOrderFlow(List<Integer> orderFlowIds, Integer dataState) {
        try {
            //空值判断
            if (dataState == null) {
                throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
            }
            if (orderFlowIds == null) {
                throw new ParamsErrorException(HintMessage.LIST_NULL);
            }
            //记录异常信息
            String msg = "";
            switch (dataState) {
                case 1:
                    msg = HintMessage.START_VALUE;
                    break;
                case 2:
                    msg = HintMessage.DELETE_VALUE;
                    break;
                case 3:
                    msg = HintMessage.STOP_VALUE;
                    break;
                default:
                    throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
            }
            //执行删除操作
            int row = orderFlowMapper.deleteOrderFlow(orderFlowIds, dataState);
            //声明msg字符串，用来存抛出的异常
            if (row == 0) {
                msg = msg + HintMessage.DEFEATED_VALUE;
            } else {
                msg = msg + HintMessage.SUCCESS_VALUE;
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 级联删除工序流程关联的工位、采集参数、关键物料、不良现象
     */
    @Transactional
    @Override
    public JSONObject deleteCascadeOrderFlow(List<Integer> orderFlowIds, Integer dataState) {
        //空值判断
        if (dataState == null) {
            throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
        }
        if (orderFlowIds == null) {
            throw new ParamsErrorException(HintMessage.LIST_NULL);
        }
        //记录异常信息
        String msg = "";
        switch (dataState) {
            case 1:
                msg = HintMessage.START_VALUE;
                break;
            case 2:
                msg = HintMessage.DELETE_VALUE;
                break;
            case 3:
                msg = HintMessage.STOP_VALUE;
                break;
            default:
                throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
        }
        //执行删除操作
        int row = orderFlowMapper.deleteOrderFlow(orderFlowIds, dataState);

        //根据传入的orderFlowIds查询对应的工位、采集参数、关键物料、不良现象集合
        for (Integer orderFlowId : orderFlowIds) {

            //级联执行工位删除
            List<OrderStation> orderStationList = orderStationMapper.queryOrderStationByOrderFlowId(orderFlowId);
            if (orderStationList != null && !orderStationList.isEmpty()) {
                for (OrderStation orderStation : orderStationList) {
                    orderStationMapper.deleteOrderStationByOrderStationId(orderStation.getOrderStationId(), dataState);
                }
            }

            //级联执行采集参数删除
            List<OrderDete> orderDeteList = orderDeteMapper.queryOrderDeteByOrderFlowId(orderFlowId);
            if (orderDeteList != null && !orderDeteList.isEmpty()) {
                for (OrderDete orderDete : orderDeteList) {
                    orderDeteMapper.deleteOrderDeteByOrderDeteId(orderDete.getOrderDeteId(), dataState);
                }
            }

            //级联执行关键物料删除
            List<OrderBom> orderBomList = orderBomMapper.queryOrderBomByOrderFlowId(orderFlowId);
            if (orderBomList != null && !orderBomList.isEmpty()) {
                for (OrderBom orderBom : orderBomList) {
                    orderBomMapper.deleteOrderBomByOrderBomId(orderBom.getOrderBomId(), dataState);
                }
            }

            //级联执行不良现象删除
            List<OrderBadpheno> orderBadphenoList = orderBadphenoMapper.queryOrderBadphenoByOrderFlowId(orderFlowId);
            if (orderBadphenoList != null && !orderBadphenoList.isEmpty()) {
                for (OrderBadpheno orderBadpheno : orderBadphenoList) {
                    orderBadphenoMapper.deleteOrderBadphenoByOrderBadphenoId(orderBadpheno.getOrderBadphenoId(), dataState);
                }
            }
        }

        //声明msg字符串，用来存抛出的异常
        if (row == 0) {
            msg = msg + HintMessage.DEFEATED_VALUE;
        } else {
            msg = msg + HintMessage.SUCCESS_VALUE;
        }
        return JSONObject.oK(msg);
    }

    @Override
    public JSONObject copyOrderStation(OrderFlow orderFlow20) {
        try {
            //判空
            if (orderFlow20 == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderFlow20.getBatchId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHID_NULL);
            }
            Integer batchId=orderFlow20.getBatchId();
            if (orderFlow20.getOrderTechId()==null){
                throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
            }
            Integer oldOrderTechId=orderFlow20.getOrderTechId();

            JSONObject jsonObject=new JSONObject();



            if(orderFlow20.getBatchId().equals(0)){
                //任务单保存
                int row= orderMapper.queryOrderTechCount(oldOrderTechId);
                if(row!=0){
                    //复制新流程
//                //查询旧版本
                Integer newOrderTechId=copyTech(oldOrderTechId,1);
                //复制流程完成
                //获取前端流程
                List<OrderStation> orderStationList9 = orderFlow20.getOrderStationList();
                //查询新本本下的工位
                //查询旧流程
                OrderFlow orderFlow21=new OrderFlow();
                orderFlow21.setOrderTechId(oldOrderTechId);
                orderFlow21.setOrderFlowId(orderFlow20.getOrderFlowId());
                List<OrderFlow> orderFlowList21 = orderFlowMapper.queryOrderFlow(orderFlow21);
                if(orderFlowList21.size()!=0){
                    orderFlow21=orderFlowList21.get(0);
                }
                //查询新流程
                OrderFlow orderFlow22=new OrderFlow();
                orderFlow22.setOrderTechId(newOrderTechId);
                List<OrderFlow> orderFlowList22 = orderFlowMapper.queryOrderFlow(orderFlow22);
                for (OrderFlow orderFlow:orderFlowList22  ) {
                    if(orderFlow.getSerialNum().equals(orderFlow21.getSerialNum())&&orderFlow.getProId().equals(orderFlow21.getProId())){
                        BeanUtils.copyProperties(orderFlow21,orderFlow22);
                        orderFlow22.setOrderFlowId(orderFlow.getOrderFlowId());
                }

                }

                Integer newOrderFlowId=orderFlow22.getOrderFlowId();

                //查询新流程
                OrderStation orderStation8=new OrderStation();
                orderStation8.setOrderFlowId(newOrderFlowId);
                List<OrderStation> orderStationList8 = orderStationMapper.queryOrderStation(orderStation8);
                //查询旧流程
                OrderStation orderStation7=new OrderStation();
                orderStation7.setOrderFlowId(orderFlow20.getOrderFlowId());
                List<OrderStation> orderStationList7 = orderStationMapper.queryOrderStation(orderStation7);
                if(orderStationList7.size()!=0) {
                    for (OrderStation orderStation6 : orderStationList9) {
                        if (!orderStation6.getOrderStationId().equals(0)) {
                            for (int i = 0; i < orderStationList7.size(); i++) {
                                if (orderStationList7.get(i).getOrderStationId().equals(orderStation6.getOrderStationId())) {
                                    orderStation6.setOrderStationId(orderStationList8.get(i).getOrderStationId());
                                }
                            }
                        }
                    }
                }
                //复制新对象

                OrderFlow   newOrderFlow=new OrderFlow();
                BeanUtils.copyProperties(orderFlow20,newOrderFlow);
                newOrderFlow.setOrderTechId(newOrderTechId);
                newOrderFlow.setOrderFlowId(newOrderFlowId);
                newOrderFlow.setOrderStationList(null);
                newOrderFlow.setOrderStationList(orderStationList9);
                //修改新流程
                jsonObject=saveOrderStation(newOrderFlow);
                }else{
                    jsonObject = saveOrderStation(orderFlow20);
                }


//
//
//







            }else {
                //批次保存
                OrderTech orderTech10 = new OrderTech();
                orderTech10.setOrderTechId(orderFlow20.getOrderTechId());
                List<OrderTech> orderTeches10 = orderTechMapper.queryOrderTech(orderTech10);
                if (orderTeches10.size() != 0) {
                    orderTech10 = orderTeches10.get(0);
                }
                System.out.println(orderTech10.getOrderTechId() + "-" + orderTech10.getSourceTypeId());
                if (orderTech10.getSourceTypeId().equals(2)) {
                    jsonObject = saveOrderStation(orderFlow20);

                } else {
                    //任务单保存

                    //复制新流程
                    //查询旧版本
                    Integer newOrderTechId=copyTech(oldOrderTechId,2);
                    //复制流程完成
                    //获取前端流程
                    List<OrderStation> orderStationList9 = orderFlow20.getOrderStationList();
                    //查询新本本下的工位
                    //查询旧流程
                    OrderFlow orderFlow21 = new OrderFlow();
                    orderFlow21.setOrderTechId(oldOrderTechId);
                    orderFlow21.setOrderFlowId(orderFlow20.getOrderFlowId());
                    List<OrderFlow> orderFlowList21 = orderFlowMapper.queryOrderFlow(orderFlow21);
                    if (orderFlowList21.size() != 0) {
                        orderFlow21 = orderFlowList21.get(0);
                    }
                    //查询新流程
                    OrderFlow orderFlow22 = new OrderFlow();
                    orderFlow22.setOrderTechId(newOrderTechId);
                    List<OrderFlow> orderFlowList22 = orderFlowMapper.queryOrderFlow(orderFlow22);
                    for (OrderFlow orderFlow : orderFlowList22) {
                        if (orderFlow.getSerialNum().equals(orderFlow21.getSerialNum()) && orderFlow.getProId().equals(orderFlow21.getProId())) {
                            BeanUtils.copyProperties(orderFlow21, orderFlow22);
                            orderFlow22.setOrderFlowId(orderFlow.getOrderFlowId());
                        }

                    }

                    Integer newOrderFlowId = orderFlow22.getOrderFlowId();

                    //查询新流程
                    OrderStation orderStation8 = new OrderStation();
                    orderStation8.setOrderFlowId(newOrderFlowId);
                    List<OrderStation> orderStationList8 = orderStationMapper.queryOrderStation(orderStation8);
                    //查询旧流程
                    OrderStation orderStation7 = new OrderStation();
                    orderStation7.setOrderFlowId(orderFlow20.getOrderFlowId());
                    List<OrderStation> orderStationList7 = orderStationMapper.queryOrderStation(orderStation7);
                    for (OrderStation orderStation6 : orderStationList9) {
                        if (!orderStation6.getOrderStationId().equals(0)) {
                            for (int i = 0; i < orderStationList7.size(); i++) {
                                if (orderStationList7.get(i).getOrderStationId().equals(orderStation6.getOrderStationId())) {
                                    orderStation6.setOrderStationId(orderStationList8.get(i).getOrderStationId());
                                }
                            }
                        }
                    }
                    //复制新对象

                    OrderFlow newOrderFlow = new OrderFlow();
                    BeanUtils.copyProperties(orderFlow20, newOrderFlow);
                    newOrderFlow.setOrderTechId(newOrderTechId);
                    newOrderFlow.setOrderFlowId(newOrderFlowId);
                    newOrderFlow.setOrderStationList(null);
                    newOrderFlow.setOrderStationList(orderStationList9);
                    //修改新流程
                    jsonObject = saveOrderStation(newOrderFlow);

                    Batch batch=new Batch();
                    batch.setBatchId(orderFlow20.getBatchId());
                    batch.setOrderTechId(newOrderTechId);
                    batchMapper.updateBatch(batch);

                }
                //更新批次流程的当前工序
                Batch batch=new Batch();
                batch.setBatchId(orderFlow20.getBatchId());
                Integer nowFlowId = orderTechMapper.selectNowFlowId(orderFlow20.getBatchId());
                if(nowFlowId==null){
                    nowFlowId=orderTechMapper.selectLastFlowId(orderFlow20.getBatchId());
                }
                batch.setOrderFlowId(nowFlowId);
                batchMapper.updateBatch(batch);
            }


            return jsonObject;
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject copyOrderBadpheno(OrderFlow orderFlow20) {
        try {
            //判空
            if (orderFlow20 == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderFlow20.getBatchId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHID_NULL);
            }
            Integer batchId=orderFlow20.getBatchId();
            if (orderFlow20.getOrderTechId()==null){
                throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
            }
            Integer oldOrderTechId=orderFlow20.getOrderTechId();

            JSONObject jsonObject=new JSONObject();



            if(orderFlow20.getBatchId().equals(0)){
                //任务单保存

                int row= orderMapper.queryOrderTechCount(oldOrderTechId);
                if(row!=0){
                    //复制新流程
               //查询旧版本
                Integer newOrderTechId=copyTech(oldOrderTechId,1);
                //复制流程完成
                //获取前端流程
                List<OrderBadpheno> orderBadphenoList9 = orderFlow20.getOrderBadphenoList();
                //查询新本本下的工位
                //查询旧流程
                OrderFlow orderFlow21=new OrderFlow();
                orderFlow21.setOrderTechId(oldOrderTechId);
                orderFlow21.setOrderFlowId(orderFlow20.getOrderFlowId());
                List<OrderFlow> orderFlowList21 = orderFlowMapper.queryOrderFlow(orderFlow21);
                if(orderFlowList21.size()!=0){
                    orderFlow21=orderFlowList21.get(0);
                }
                //查询新流程
                OrderFlow orderFlow22=new OrderFlow();
                orderFlow22.setOrderTechId(newOrderTechId);
                List<OrderFlow> orderFlowList22 = orderFlowMapper.queryOrderFlow(orderFlow22);
                for (OrderFlow orderFlow:orderFlowList22  ) {
                    if(orderFlow.getSerialNum().equals(orderFlow21.getSerialNum())&&orderFlow.getProId().equals(orderFlow21.getProId())){
                        BeanUtils.copyProperties(orderFlow21,orderFlow22);
                        orderFlow22.setOrderFlowId(orderFlow.getOrderFlowId());
                    }

                }

                Integer newOrderFlowId=orderFlow22.getOrderFlowId();

                //查询新流程
                OrderBadpheno orderBadpheno8=new OrderBadpheno();
                orderBadpheno8.setOrderFlowId(newOrderFlowId);
                List<OrderBadpheno> orderBadphenoList8 = orderBadphenoMapper.queryOrderBadpheno(orderBadpheno8);
                //查询旧流程
                OrderBadpheno orderBadpheno7=new OrderBadpheno();
                orderBadpheno7.setOrderFlowId(orderFlow20.getOrderFlowId());
                List<OrderBadpheno> orderBadphenoList7 = orderBadphenoMapper.queryOrderBadpheno(orderBadpheno7);
                if(orderBadphenoList7.size()!=0) {
                    for (OrderBadpheno orderBadpheno6 : orderBadphenoList9) {
                        if (!orderBadpheno6.getOrderBadphenoId().equals(0)) {
                            for (int i = 0; i < orderBadphenoList7.size(); i++) {
                                if (orderBadphenoList7.get(i).getOrderBadphenoId().equals(orderBadpheno6.getOrderBadphenoId())) {
                                    orderBadpheno6.setOrderBadphenoId(orderBadphenoList8.get(i).getOrderBadphenoId());
                                }
                            }
                        }
                    }
                }
                //复制新对象

                OrderFlow   newOrderFlow=new OrderFlow();
                BeanUtils.copyProperties(orderFlow20,newOrderFlow);
                newOrderFlow.setOrderTechId(newOrderTechId);
                newOrderFlow.setOrderFlowId(newOrderFlowId);
                newOrderFlow.setOrderBadphenoList(null);
                newOrderFlow.setOrderBadphenoList(orderBadphenoList9);
                //修改新流程
                jsonObject=saveOrderBadpheno(newOrderFlow);

                }else{
                    jsonObject = saveOrderBadpheno(orderFlow20);
                }

//






            }else {
                //批次保存
                OrderTech orderTech10 = new OrderTech();
                orderTech10.setOrderTechId(orderFlow20.getOrderTechId());
                List<OrderTech> orderTeches10 = orderTechMapper.queryOrderTech(orderTech10);
                if (orderTeches10.size() != 0) {
                    orderTech10 = orderTeches10.get(0);
                }
                System.out.println(orderTech10.getOrderTechId() + "-" + orderTech10.getSourceTypeId());
                if (orderTech10.getSourceTypeId().equals(2)) {
                    jsonObject = saveOrderBadpheno(orderFlow20);

                } else {
                    //任务单保存

                    //复制新流程
                    Integer newOrderTechId=copyTech(oldOrderTechId,2);
                    //复制流程完成
                    //获取前端流程
                    List<OrderBadpheno> orderBadphenoList9 = orderFlow20.getOrderBadphenoList();
                    //查询新本本下的工位
                    //查询旧流程
                    OrderFlow orderFlow21 = new OrderFlow();
                    orderFlow21.setOrderTechId(oldOrderTechId);
                    orderFlow21.setOrderFlowId(orderFlow20.getOrderFlowId());
                    List<OrderFlow> orderFlowList21 = orderFlowMapper.queryOrderFlow(orderFlow21);
                    if (orderFlowList21.size() != 0) {
                        orderFlow21 = orderFlowList21.get(0);
                    }
                    //查询新流程
                    OrderFlow orderFlow22 = new OrderFlow();
                    orderFlow22.setOrderTechId(newOrderTechId);
                    List<OrderFlow> orderFlowList22 = orderFlowMapper.queryOrderFlow(orderFlow22);
                    for (OrderFlow orderFlow : orderFlowList22) {
                        if (orderFlow.getSerialNum().equals(orderFlow21.getSerialNum()) && orderFlow.getProId().equals(orderFlow21.getProId())) {
                            BeanUtils.copyProperties(orderFlow21, orderFlow22);
                            orderFlow22.setOrderFlowId(orderFlow.getOrderFlowId());
                        }

                    }

                    Integer newOrderFlowId = orderFlow22.getOrderFlowId();

                    //查询新流程
                    OrderBadpheno orderBadpheno8 = new OrderBadpheno();
                    orderBadpheno8.setOrderFlowId(newOrderFlowId);
                    List<OrderBadpheno> orderBadphenoList8 = orderBadphenoMapper.queryOrderBadpheno(orderBadpheno8);
                    //查询旧流程
                    OrderBadpheno orderBadpheno7 = new OrderBadpheno();
                    orderBadpheno7.setOrderFlowId(orderFlow20.getOrderFlowId());
                    List<OrderBadpheno> orderBadphenoList7 = orderBadphenoMapper.queryOrderBadpheno(orderBadpheno7);
                    for (OrderBadpheno orderBadpheno6 : orderBadphenoList9) {
                        if (!orderBadpheno6.getOrderBadphenoId().equals(0)) {
                            for (int i = 0; i < orderBadphenoList7.size(); i++) {
                                if (orderBadphenoList7.get(i).getOrderBadphenoId().equals(orderBadpheno6.getOrderBadphenoId())) {
                                    orderBadpheno6.setOrderBadphenoId(orderBadphenoList8.get(i).getOrderBadphenoId());
                                }
                            }
                        }
                    }
                    //复制新对象

                    OrderFlow newOrderFlow = new OrderFlow();
                    BeanUtils.copyProperties(orderFlow20, newOrderFlow);
                    newOrderFlow.setOrderTechId(newOrderTechId);
                    newOrderFlow.setOrderFlowId(newOrderFlowId);
                    newOrderFlow.setOrderBadphenoList(null);
                    newOrderFlow.setOrderBadphenoList(orderBadphenoList9);
                    //修改新流程
                    jsonObject = saveOrderBadpheno(newOrderFlow);

                    Batch batch=new Batch();
                    batch.setBatchId(orderFlow20.getBatchId());
                    batch.setOrderTechId(newOrderTechId);
                    batchMapper.updateBatch(batch);
                }
                //更新批次流程的当前工序
                Batch batch=new Batch();
                batch.setBatchId(orderFlow20.getBatchId());
                Integer nowFlowId = orderTechMapper.selectNowFlowId(orderFlow20.getBatchId());
                if(nowFlowId==null){
                    nowFlowId=orderTechMapper.selectLastFlowId(orderFlow20.getBatchId());
                }
                batch.setOrderFlowId(nowFlowId);
                batchMapper.updateBatch(batch);
            }


            return jsonObject;
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject copyOrderBom(OrderFlow orderFlow20) {
        try {
            //判空
            if (orderFlow20 == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderFlow20.getBatchId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHID_NULL);
            }
            Integer batchId=orderFlow20.getBatchId();
            if (orderFlow20.getOrderTechId()==null){
                throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
            }
            Integer oldOrderTechId=orderFlow20.getOrderTechId();

            JSONObject jsonObject=new JSONObject();



            if(orderFlow20.getBatchId().equals(0)){
                //任务单保存

                int row= orderMapper.queryOrderTechCount(oldOrderTechId);
                if(row!=0){
                    //查询旧版本
                Integer newOrderTechId=copyTech(oldOrderTechId,1);
                //复制流程完成
                //获取前端流程
                List<OrderBom> orderBomList9 = orderFlow20.getOrderBomList();
                //查询新本本下的工位
                //查询旧流程
                OrderFlow orderFlow21=new OrderFlow();
                orderFlow21.setOrderTechId(oldOrderTechId);
                orderFlow21.setOrderFlowId(orderFlow20.getOrderFlowId());
                List<OrderFlow> orderFlowList21 = orderFlowMapper.queryOrderFlow(orderFlow21);
                if(orderFlowList21.size()!=0){
                    orderFlow21=orderFlowList21.get(0);
                }
                //查询新流程
                OrderFlow orderFlow22=new OrderFlow();
                orderFlow22.setOrderTechId(newOrderTechId);
                List<OrderFlow> orderFlowList22 = orderFlowMapper.queryOrderFlow(orderFlow22);
                for (OrderFlow orderFlow:orderFlowList22  ) {
                    if(orderFlow.getSerialNum().equals(orderFlow21.getSerialNum())&&orderFlow.getProId().equals(orderFlow21.getProId())){
                        BeanUtils.copyProperties(orderFlow21,orderFlow22);
                        orderFlow22.setOrderFlowId(orderFlow.getOrderFlowId());
                    }

                }

                Integer newOrderFlowId=orderFlow22.getOrderFlowId();

                //查询新流程
                OrderBom orderBom8=new OrderBom();
                orderBom8.setOrderFlowId(newOrderFlowId);
                List<OrderBom> orderBomList8 = orderBomMapper.queryOrderBom(orderBom8);
                //查询旧流程
                OrderBom orderBom7=new OrderBom();
                orderBom7.setOrderFlowId(orderFlow20.getOrderFlowId());
                List<OrderBom> orderBomList7 = orderBomMapper.queryOrderBom(orderBom7);
                if(orderBomList7.size()!=0) {
                    for (OrderBom orderBom6 : orderBomList9) {
                        if (!orderBom6.getOrderBomId().equals(0)) {
                            for (int i = 0; i < orderBomList7.size(); i++) {
                                if (orderBomList7.get(i).getOrderBomId().equals(orderBom6.getOrderBomId())) {
                                    orderBom6.setOrderBomId(orderBomList8.get(i).getOrderBomId());
                                }
                            }
                        }
                    }
                }
                //复制新对象

                OrderFlow   newOrderFlow=new OrderFlow();
                BeanUtils.copyProperties(orderFlow20,newOrderFlow);
                newOrderFlow.setOrderTechId(newOrderTechId);
                newOrderFlow.setOrderFlowId(newOrderFlowId);
                newOrderFlow.setOrderBomList(null);
                newOrderFlow.setOrderBomList(orderBomList9);
                //修改新流程
                jsonObject=saveOrderBom(newOrderFlow);
                }else{
                    jsonObject = saveOrderBom(orderFlow20);
                }

                //复制新流程
//









            }else {
                //批次保存
                OrderTech orderTech10 = new OrderTech();
                orderTech10.setOrderTechId(orderFlow20.getOrderTechId());
                List<OrderTech> orderTeches10 = orderTechMapper.queryOrderTech(orderTech10);
                if (orderTeches10.size() != 0) {
                    orderTech10 = orderTeches10.get(0);
                }
                System.out.println(orderTech10.getOrderTechId() + "-" + orderTech10.getSourceTypeId());
                if (orderTech10.getSourceTypeId().equals(2)) {
                    jsonObject = saveOrderBom(orderFlow20);

                } else {
                    //任务单保存

                    //复制新流程
                    Integer newOrderTechId=copyTech(oldOrderTechId,2);
                    //复制流程完成
                    //获取前端流程
                    List<OrderBom> orderBomList9 = orderFlow20.getOrderBomList();
                    //查询新本本下的工位
                    //查询旧流程
                    OrderFlow orderFlow21 = new OrderFlow();
                    orderFlow21.setOrderTechId(oldOrderTechId);
                    orderFlow21.setOrderFlowId(orderFlow20.getOrderFlowId());
                    List<OrderFlow> orderFlowList21 = orderFlowMapper.queryOrderFlow(orderFlow21);
                    if (orderFlowList21.size() != 0) {
                        orderFlow21 = orderFlowList21.get(0);
                    }
                    //查询新流程
                    OrderFlow orderFlow22 = new OrderFlow();
                    orderFlow22.setOrderTechId(newOrderTechId);
                    List<OrderFlow> orderFlowList22 = orderFlowMapper.queryOrderFlow(orderFlow22);
                    for (OrderFlow orderFlow : orderFlowList22) {
                        if (orderFlow.getSerialNum().equals(orderFlow21.getSerialNum()) && orderFlow.getProId().equals(orderFlow21.getProId())) {
                            BeanUtils.copyProperties(orderFlow21, orderFlow22);
                            orderFlow22.setOrderFlowId(orderFlow.getOrderFlowId());
                        }

                    }

                    Integer newOrderFlowId = orderFlow22.getOrderFlowId();

                    //查询新流程
                    OrderBom orderBom8 = new OrderBom();
                    orderBom8.setOrderFlowId(newOrderFlowId);
                    List<OrderBom> orderBomList8 = orderBomMapper.queryOrderBom(orderBom8);
                    //查询旧流程
                    OrderBom orderBom7 = new OrderBom();
                    orderBom7.setOrderFlowId(orderFlow20.getOrderFlowId());
                    List<OrderBom> orderBomList7 = orderBomMapper.queryOrderBom(orderBom7);
                    for (OrderBom orderBom6 : orderBomList9) {
                        if (!orderBom6.getOrderBomId().equals(0)) {
                            for (int i = 0; i < orderBomList7.size(); i++) {
                                if (orderBomList7.get(i).getOrderBomId().equals(orderBom6.getOrderBomId())) {
                                    orderBom6.setOrderBomId(orderBomList8.get(i).getOrderBomId());
                                }
                            }
                        }
                    }
                    //复制新对象

                    OrderFlow newOrderFlow = new OrderFlow();
                    BeanUtils.copyProperties(orderFlow20, newOrderFlow);
                    newOrderFlow.setOrderTechId(newOrderTechId);
                    newOrderFlow.setOrderFlowId(newOrderFlowId);
                    newOrderFlow.setOrderBomList(null);
                    newOrderFlow.setOrderBomList(orderBomList9);
                    //修改新流程
                    jsonObject = saveOrderBom(newOrderFlow);

                    Batch batch=new Batch();
                    batch.setBatchId(orderFlow20.getBatchId());
                    batch.setOrderTechId(newOrderTechId);
                    batchMapper.updateBatch(batch);
                }
                //更新批次流程的当前工序
                Batch batch=new Batch();
                batch.setBatchId(orderFlow20.getBatchId());
                Integer nowFlowId = orderTechMapper.selectNowFlowId(orderFlow20.getBatchId());
                if(nowFlowId==null){
                    nowFlowId=orderTechMapper.selectLastFlowId(orderFlow20.getBatchId());
                }
                batch.setOrderFlowId(nowFlowId);
                batchMapper.updateBatch(batch);
            }


            return jsonObject;
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject copyOrderDete(OrderFlow orderFlow20) {
        try {
            //判空
            if (orderFlow20 == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderFlow20.getBatchId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHID_NULL);
            }
            Integer batchId=orderFlow20.getBatchId();
            if (orderFlow20.getOrderTechId()==null){
                throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
            }
            Integer oldOrderTechId=orderFlow20.getOrderTechId();

            JSONObject jsonObject=new JSONObject();



            if(orderFlow20.getBatchId().equals(0)){
                //任务单保存

                //复制新流程
                //查询旧版本

                int row= orderMapper.queryOrderTechCount(oldOrderTechId);
                if(row!=0){
                    Integer newOrderTechId=copyTech(oldOrderTechId,1);
                //复制流程完成
                //获取前端流程
                List<OrderDete> orderDeteList9 = orderFlow20.getOrderDeteList();
                //查询新本本下的工位
                //查询旧流程
                OrderFlow orderFlow21=new OrderFlow();
                orderFlow21.setOrderTechId(oldOrderTechId);
                orderFlow21.setOrderFlowId(orderFlow20.getOrderFlowId());
                List<OrderFlow> orderFlowList21 = orderFlowMapper.queryOrderFlow(orderFlow21);
                if(orderFlowList21.size()!=0){
                    orderFlow21=orderFlowList21.get(0);
                }
                //查询新流程
                OrderFlow orderFlow22=new OrderFlow();
                orderFlow22.setOrderTechId(newOrderTechId);
                List<OrderFlow> orderFlowList22 = orderFlowMapper.queryOrderFlow(orderFlow22);
                for (OrderFlow orderFlow:orderFlowList22  ) {
                    if(orderFlow.getSerialNum().equals(orderFlow21.getSerialNum())&&orderFlow.getProId().equals(orderFlow21.getProId())){
                        BeanUtils.copyProperties(orderFlow21,orderFlow22);
                        orderFlow22.setOrderFlowId(orderFlow.getOrderFlowId());
                    }

                }

                Integer newOrderFlowId=orderFlow22.getOrderFlowId();

                //查询新流程
                OrderDete orderDete8=new OrderDete();
                orderDete8.setOrderFlowId(newOrderFlowId);
                List<OrderDete> orderDeteList8 = orderDeteMapper.queryOrderDete(orderDete8);
                //查询旧流程
                OrderDete orderDete7=new OrderDete();
                orderDete7.setOrderFlowId(orderFlow20.getOrderFlowId());
                List<OrderDete> orderDeteList7 = orderDeteMapper.queryOrderDete(orderDete7);
                if(orderDeteList7.size()!=0) {
                    for (OrderDete orderDete6 : orderDeteList9) {
                        if (!orderDete6.getOrderDeteId().equals(0)) {
                            for (int i = 0; i < orderDeteList7.size(); i++) {
                                if (orderDeteList7.get(i).getOrderDeteId().equals(orderDete6.getOrderDeteId())) {
                                    orderDete6.setOrderDeteId(orderDeteList8.get(i).getOrderDeteId());
                                }
                            }
                        }
                    }
                }
                //复制新对象

                OrderFlow   newOrderFlow=new OrderFlow();
                BeanUtils.copyProperties(orderFlow20,newOrderFlow);
                newOrderFlow.setOrderTechId(newOrderTechId);
                newOrderFlow.setOrderFlowId(newOrderFlowId);
                newOrderFlow.setOrderDeteList(null);
                newOrderFlow.setOrderDeteList(orderDeteList9);
                //修改新流程
                jsonObject=saveOrderDete(newOrderFlow);
                }else{
                    jsonObject = saveOrderDete(orderFlow20);
                }


//









            }else {
                //批次保存
                OrderTech orderTech10 = new OrderTech();
                orderTech10.setOrderTechId(orderFlow20.getOrderTechId());
                List<OrderTech> orderTeches10 = orderTechMapper.queryOrderTech(orderTech10);
                if (orderTeches10.size() != 0) {
                    orderTech10 = orderTeches10.get(0);
                }
                System.out.println(orderTech10.getOrderTechId() + "-" + orderTech10.getSourceTypeId());
                if (orderTech10.getSourceTypeId().equals(2)) {
                    jsonObject = saveOrderDete(orderFlow20);

                } else {
                    //任务单保存

                    //复制新流程
                    //查询旧版本
                    //复制版本
                    Integer newOrderTechId=copyTech(oldOrderTechId,2);
                    //复制流程完成
                    //获取前端流程
                    List<OrderDete> orderDeteList9 = orderFlow20.getOrderDeteList();
                    //查询新本本下的工位
                    //查询旧流程
                    OrderFlow orderFlow21 = new OrderFlow();
                    orderFlow21.setOrderTechId(oldOrderTechId);
                    orderFlow21.setOrderFlowId(orderFlow20.getOrderFlowId());
                    List<OrderFlow> orderFlowList21 = orderFlowMapper.queryOrderFlow(orderFlow21);
                    if (orderFlowList21.size() != 0) {
                        orderFlow21 = orderFlowList21.get(0);
                    }
                    //查询新流程
                    OrderFlow orderFlow22 = new OrderFlow();
                    orderFlow22.setOrderTechId(newOrderTechId);
                    List<OrderFlow> orderFlowList22 = orderFlowMapper.queryOrderFlow(orderFlow22);
                    for (OrderFlow orderFlow : orderFlowList22) {
                        if (orderFlow.getSerialNum().equals(orderFlow21.getSerialNum()) && orderFlow.getProId().equals(orderFlow21.getProId())) {
                            BeanUtils.copyProperties(orderFlow21, orderFlow22);
                            orderFlow22.setOrderFlowId(orderFlow.getOrderFlowId());
                        }

                    }

                    Integer newOrderFlowId = orderFlow22.getOrderFlowId();

                    //查询新流程
                    OrderDete orderDete8 = new OrderDete();
                    orderDete8.setOrderFlowId(newOrderFlowId);
                    List<OrderDete> orderDeteList8 = orderDeteMapper.queryOrderDete(orderDete8);
                    //查询旧流程
                    OrderDete orderDete7 = new OrderDete();
                    orderDete7.setOrderFlowId(orderFlow20.getOrderFlowId());
                    List<OrderDete> orderDeteList7 = orderDeteMapper.queryOrderDete(orderDete7);
                    for (OrderDete orderDete6 : orderDeteList9) {
                        if (!orderDete6.getOrderDeteId().equals(0)) {
                            for (int i = 0; i < orderDeteList7.size(); i++) {
                                if (orderDeteList7.get(i).getOrderDeteId().equals(orderDete6.getOrderDeteId())) {
                                    orderDete6.setOrderDeteId(orderDeteList8.get(i).getOrderDeteId());
                                }
                            }
                        }
                    }
                    //复制新对象

                    OrderFlow newOrderFlow = new OrderFlow();
                    BeanUtils.copyProperties(orderFlow20, newOrderFlow);
                    newOrderFlow.setOrderTechId(newOrderTechId);
                    newOrderFlow.setOrderFlowId(newOrderFlowId);
                    newOrderFlow.setOrderDeteList(null);
                    newOrderFlow.setOrderDeteList(orderDeteList9);
                    //修改新流程
                    jsonObject = saveOrderDete(newOrderFlow);

                    Batch batch=new Batch();
                    batch.setBatchId(orderFlow20.getBatchId());
                    batch.setOrderTechId(newOrderTechId);
                    batchMapper.updateBatch(batch);
                }

                //更新批次流程的当前工序
                Batch batch=new Batch();
                batch.setBatchId(orderFlow20.getBatchId());
                Integer nowFlowId = orderTechMapper.selectNowFlowId(orderFlow20.getBatchId());
                if(nowFlowId==null){
                    nowFlowId=orderTechMapper.selectLastFlowId(orderFlow20.getBatchId());
                }
                batch.setOrderFlowId(nowFlowId);
                batchMapper.updateBatch(batch);
            }


            return jsonObject;
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 判重
     */
    public void checkParam(OrderFlow orderFlow) {

        //工序ID
        Integer proId = orderFlow.getProId();
        //并序状态
        Integer sequence = orderFlow.getSequence();
        //顺序号
        Integer serialNum = orderFlow.getSerialNum();
        //版本ID
        Integer orderTechId = orderFlow.getOrderTechId();

        //物料ID
        if (proId == null) {
            throw new ParamsErrorException(HintMessage.PROID_NULL);
        }
        //并序状态
        if (sequence == null) {
            throw new ParamsErrorException(HintMessage.SEQUENCE_NULL);
        }
        //顺序号
        if (serialNum == null) {
            throw new ParamsErrorException(HintMessage.SERIALNUM_NULL);
        }
        //版本ID
        if (orderTechId == null) {
            throw new ParamsErrorException(HintMessage.ORDERTECHID_NULL);
        }
        // 执行判重操作
        Integer Counts = orderFlowMapper.checkOrderFlow(orderFlow);
        if (Counts != 0) {
            throw new ParamsErrorException(HintMessage.REPEATING_DATA);
        }

    }

    /**
     * 工位保存
     */
    @Override
    public JSONObject saveOrderStation(OrderFlow orderFlow) {
        try {
            //判空
            if (orderFlow == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderFlow.getOrderFlowId() == null) {
                throw new ParamsErrorException(HintMessage.ORDERFLOWID_NULL);
            }

            //获取流程id
            Integer orderFlowId = orderFlow.getOrderFlowId();
            //获取list集合
            List<OrderStation> orderStationList = new ArrayList<>();
            orderStationList = orderFlow.getOrderStationList();
            for (OrderStation orderStation : orderStationList) {
                orderStation.setOrderFlowId(orderFlowId);
            }


            //去除不要的工位
            //反查流程
            OrderFlow orderf=orderFlowMapper.queryOrderFlowByOrderFlowId(orderFlowId);
            List<OrderFlow> orderFlowList = orderFlowMapper.queryOrderFlowByOrderTechId(orderf.getOrderTechId());
            List<OrderFlow> orderFlowList1 =new ArrayList<>();


            //提出相同工序
            if(orderFlowList.size()!=0){
                for (OrderFlow of:orderFlowList ) {
                    if(orderf.getProId().equals(of.getProId())){
                        orderFlowList1.add(of);
                    }
                }
            }
            orderFlowList.removeAll(orderFlowList1);
            //查询流程下所有工位
            String strStation="";
            List<OrderStation> orderStationList1 = orderStationMapper.queryOrderStationByOrderFlowList(orderFlowList);
            List<OrderStation> orderStationList2  =new ArrayList<>();
            for (OrderStation station:orderStationList1  ) {
                for (OrderStation station1:orderStationList ) {
                    if(station.getStationId().equals(station1.getStationId())){
                        orderStationList2.add(station1);
                        strStation=strStation+station.getStationId()+" ";
                    }
                }

            }
            orderStationList.removeAll(orderStationList2);

            //获取数据库数据
            OrderStation orderStation1 = new OrderStation();
            orderStation1.setOrderFlowId(orderFlowId);
            List<OrderStation> oldorderStationList = orderStationMapper.queryOrderStation(orderStation1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldorderStationList != null) {
                for (OrderStation orderStation : oldorderStationList) {
                    oldIds.add(orderStation.getOrderStationId());
                }
            }
            if (orderStationList != null) {
                for (OrderStation orderStation : orderStationList) {
                    newIds.add(orderStation.getOrderStationId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = orderStationMapper.deleteOrderStation(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (orderStationList != null) {
                for (OrderStation orderStation : orderStationList) {
                    if (orderStation.getDataState() == null) {
                        orderStation.setDataState(1);
                    }

                    if (orderStation.getOrderStationId() != null) {
                        if (orderStation.getOrderStationId().equals(0)) {
                            orderStation.setOrderStationId(null);
                            // 执行判重操作
                            Integer Counts = orderStationMapper.checkOrderStation(orderStation);
                            if (Counts == 0) {
                                insertrow = insertrow + orderStationMapper.insertOrderStation(orderStation);
                            }

                        } else {
                            // 执行判重操作
                            Integer Counts = orderStationMapper.checkOrderStation(orderStation);
                            if (Counts == 0) {
                                updaterow = updaterow + orderStationMapper.updateOrderStation(orderStation);
                            }
                        }
                    }
                }
            }

//            if(strStation.equals("")){
                return JSONObject.oK(HintMessage.SAVE_SUCCESS, orderFlow,deleterow + insertrow + updaterow);
//            }else{
//                return JSONObject.oK(strStation+HintMessage.INSERT_DEFEATED, deleterow + insertrow + updaterow);
//            }


        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 采集参数保存
     */
    @Override
    public JSONObject saveOrderDete(OrderFlow orderFlow) {
        try {
            //判空
            if (orderFlow == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderFlow.getOrderFlowId() == null) {
                throw new ParamsErrorException(HintMessage.ORDERFLOWID_NULL);
            }
            //获取流程id
            Integer orderFlowId = orderFlow.getOrderFlowId();
            //获取list集合
            List<OrderDete> orderDeteList = new ArrayList<>();
            orderDeteList = orderFlow.getOrderDeteList();
            for (OrderDete orderDete : orderDeteList) {
                orderDete.setOrderFlowId(orderFlowId);
            }


            //获取数据库数据
            OrderDete orderDete1 = new OrderDete();
            orderDete1.setOrderFlowId(orderFlowId);
            List<OrderDete> oldorderDeteList = orderDeteMapper.queryOrderDete(orderDete1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldorderDeteList != null) {
                for (OrderDete orderDete : oldorderDeteList) {
                    oldIds.add(orderDete.getOrderDeteId());
                }
            }
            if (orderDeteList != null) {
                for (OrderDete orderDete : orderDeteList) {
                    newIds.add(orderDete.getOrderDeteId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = orderDeteMapper.deleteOrderDete(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (orderDeteList != null) {
                for (OrderDete orderDete : orderDeteList) {
                    if (orderDete.getDataState() == null) {
                        orderDete.setDataState(1);
                    }

                    if (orderDete.getOrderDeteId() != null) {
                        if (orderDete.getOrderDeteId().equals(0)) {
                            orderDete.setOrderDeteId(null);
                            // 执行判重操作
                            Integer Counts = orderDeteMapper.checkOrderDete(orderDete);
                            if (Counts == 0) {
                                insertrow = insertrow + orderDeteMapper.insertOrderDete(orderDete);
                            }
                        } else {
                            // 执行判重操作
                            Integer Counts = orderDeteMapper.checkOrderDete(orderDete);
                            if (Counts == 0) {
                                updaterow = updaterow + orderDeteMapper.updateOrderDete(orderDete);
                            }
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, orderFlow,deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 物料保存
     */
    @Override
    public JSONObject saveOrderBom(OrderFlow orderFlow) {
        try {
            //判空
            if (orderFlow == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderFlow.getOrderFlowId() == null) {
                throw new ParamsErrorException(HintMessage.ORDERFLOWID_NULL);
            }
            //获取流程id
            Integer orderFlowId = orderFlow.getOrderFlowId();
            //获取list集合
            List<OrderBom> orderBomList = new ArrayList<>();
            orderBomList = orderFlow.getOrderBomList();
            for (OrderBom orderBom : orderBomList) {
                orderBom.setOrderFlowId(orderFlowId);
            }


            //获取数据库数据
            OrderBom orderBom1 = new OrderBom();
            orderBom1.setOrderFlowId(orderFlowId);
            List<OrderBom> oldorderBomList = orderBomMapper.queryOrderBom(orderBom1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldorderBomList != null) {
                for (OrderBom orderBom : oldorderBomList) {
                    oldIds.add(orderBom.getOrderBomId());
                }
            }
            if (orderBomList != null) {
                for (OrderBom orderBom : orderBomList) {
                    newIds.add(orderBom.getOrderBomId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = orderBomMapper.deleteOrderBom(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (orderBomList != null) {
                for (OrderBom orderBom : orderBomList) {
                    if (orderBom.getDataState() == null) {
                        orderBom.setDataState(1);
                    }
                    if (orderBom.getOrderBomId() != null) {
                        if (orderBom.getOrderBomId().equals(0)) {
                            orderBom.setOrderBomId(null);
                            // 执行判重操作
                            Integer Counts = orderBomMapper.checkOrderBom(orderBom);
                            if (Counts == 0) {
                                insertrow = insertrow + orderBomMapper.insertOrderBom(orderBom);
                            }

                        } else {
                            // 执行判重操作
                            Integer Counts = orderBomMapper.checkOrderBom(orderBom);
                            if (Counts == 0) {
                                updaterow = updaterow + orderBomMapper.updateOrderBom(orderBom);
                            }
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, orderFlow,deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 不良现象保存
     */
    @Override
    public JSONObject saveOrderBadpheno(OrderFlow orderFlow) {
        try {
            //判空
            if (orderFlow == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (orderFlow.getOrderFlowId() == null) {
                throw new ParamsErrorException(HintMessage.ORDERFLOWID_NULL);
            }
            //获取流程id
            Integer orderFlowId = orderFlow.getOrderFlowId();

            //获取list集合
            List<OrderBadpheno> orderBadphenoList = new ArrayList<>();
            orderBadphenoList = orderFlow.getOrderBadphenoList();
            for (OrderBadpheno orderBadpheno : orderBadphenoList) {
                orderBadpheno.setOrderFlowId(orderFlowId);
            }

            //获取数据库数据
            OrderBadpheno orderBadpheno1 = new OrderBadpheno();
            orderBadpheno1.setOrderFlowId(orderFlowId);
            List<OrderBadpheno> oldorderBadphenoList = orderBadphenoMapper.queryOrderBadpheno(orderBadpheno1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldorderBadphenoList != null) {
                for (OrderBadpheno orderBadpheno : oldorderBadphenoList) {
                    oldIds.add(orderBadpheno.getOrderBadphenoId());
                }
            }
            if (orderBadphenoList != null) {
                for (OrderBadpheno orderBadpheno : orderBadphenoList) {
                    newIds.add(orderBadpheno.getOrderBadphenoId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = orderBadphenoMapper.deleteOrderBadpheno(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (orderBadphenoList != null) {
                for (OrderBadpheno orderBadpheno : orderBadphenoList) {
                    if (orderBadpheno.getDataState() == null) {
                        orderBadpheno.setDataState(1);
                    }


                    if (orderBadpheno.getOrderBadphenoId() != null) {
                        if (orderBadpheno.getOrderBadphenoId().equals(0)) {
                            orderBadpheno.setOrderBadphenoId(null);
                            // 执行判重操作
                            Integer Counts = orderBadphenoMapper.checkOrderBadpheno(orderBadpheno);
                            if (Counts == 0) {
                                insertrow = insertrow + orderBadphenoMapper.insertOrderBadpheno(orderBadpheno);
                            }

                        } else {
                            // 执行判重操作
                            Integer Counts = orderBadphenoMapper.checkOrderBadpheno(orderBadpheno);
                            if (Counts == 0) {
                                updaterow = updaterow + orderBadphenoMapper.updateOrderBadpheno(orderBadpheno);
                            }
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, orderFlow,deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * @Author 熊冰  产品流程导入任务单流程
     * @Date 2019/12/4 14:47
     */
    @Override
    public JSONObject leadOrderFlow(Integer matVerId, Integer orderId) {
        try {
            //判空
            if (matVerId == null) {
                throw new ParamsErrorException(HintMessage.MATVERID_NULL);
            }
            if (orderId == null) {
                throw new ParamsErrorException(HintMessage.ORDERID_NULL);
            }
            //创建新的版本id
            OrderTech orderTech=new OrderTech();
            orderTech.setOrderId(orderId);
            orderTech.setSourceTypeId(1);
            orderTech.setUserId(1);
            orderTech.setDataState(1);

			orderTechMapper.insertOrderTech(orderTech);
			Integer orderTechId = orderTech.getOrderTechId();

			//查询旧的流程
            MatFlow matFlow1 = new MatFlow();
            matFlow1.setMatVerId(matVerId);
            List<MatFlow> matFlowList = matFlowMapper.queryMatFlow(matFlow1);
            for (MatFlow matFlow : matFlowList) {
                //新增流程
                Integer matFlowId = matFlow.getMatFlowId();
                OrderFlow orderFlow = new OrderFlow();
                orderFlow.setNextPro(matFlow.getNextPro());
                orderFlow.setOrderFlowId(null);
                orderFlow.setOrderTechId(orderTechId);
                orderFlow.setProId(matFlow.getProId());
                orderFlow.setSequence(matFlow.getSequence());
                orderFlow.setSerialNum(matFlow.getSerialNum());
                orderFlow.setRisePro(matFlow.getRisePro());
                orderFlow.setDataState(1);
                orderFlowMapper.insertOrderFlow(orderFlow);
                Integer orderFlowId = orderFlow.getOrderFlowId();

                //新增工位
                MatStation matStation1 = new MatStation();
                matStation1.setMatFlowId(matFlowId);
                List<MatStation> matStationList = matStationMapper.queryMatStation(matStation1);
                for (MatStation matStation : matStationList) {
                    OrderStation orderStation = new OrderStation();
                    orderStation.setOrderFlowId(orderFlowId);
                    orderStation.setStationId(matStation.getStationId());
                    orderStation.setDataState(1);
                    orderStationMapper.insertOrderStation(orderStation);
                }
                //新增不良
                MatBadpheno matBadpheno1 = new MatBadpheno();
                matBadpheno1.setMatFlowId(matFlowId);
                List<MatBadpheno> matBadphenoList = matBadphenoMapper.queryMatBadpheno(matBadpheno1);
                for (MatBadpheno matBadpheno : matBadphenoList) {
                    OrderBadpheno orderBadpheno = new OrderBadpheno();
                    orderBadpheno.setOrderFlowId(orderFlowId);
                    orderBadpheno.setBadphenoId(matBadpheno.getBadphenoId());
                    orderBadpheno.setDataState(1);
                    orderBadphenoMapper.insertOrderBadpheno(orderBadpheno);
                }

                //新增关键物料
                MatBom matBom1 = new MatBom();
                matBom1.setMatFlowId(matFlowId);
                List<MatBom> matBomList = matBomMapper.queryMatBom(matBom1);
                for (MatBom matBom : matBomList) {
                    OrderBom orderBom = new OrderBom();
                    orderBom.setOrderFlowId(orderFlowId);
                    orderBom.setMatId(matBom.getMatId());
                    orderBom.setReduceTypeId(matBom.getReduceTypeId());
                    orderBom.setReduceNum(matBom.getReduceNum());
                    orderBom.setKeyMaterial(matBom.getKeyMaterial());
                    orderBom.setDataState(1);
                    orderBomMapper.insertOrderBom(orderBom);
                }
                //新增采集项目
                MatDete matDete1 = new MatDete();
                matDete1.setMatFlowId(matFlowId);
                List<MatDete> matDeteList = matDeteMapper.queryMatDete(matDete1);
                for (MatDete matDete : matDeteList) {
                    OrderDete orderDete = new OrderDete();
                    orderDete.setOrderFlowId(orderFlowId);
                    orderDete.setDeteId(matDete.getDeteId());
                    orderDete.setLowerLimit(matDete.getLowerLimit());
                    orderDete.setMiddleLimit(matDete.getMiddleLimit());
                    orderDete.setUpperLimit(matDete.getUpperLimit());
                    orderDete.setSerialNum(matDete.getSerialNum());
                    orderDete.setSampleNum(matDete.getSampleNum());
                    orderDete.setSampleCount(matDete.getSampleCount());
                    orderDete.setShowDete(matDete.getShowDete());
                    orderDete.setDataState(1);
                    orderDeteMapper.insertOrderDete(orderDete);
                }


            }

            //查询旧的维修流程
            MatRepair matRepair1 = new MatRepair();
            matRepair1.setMatVerId(matVerId);
            List<MatRepair> matRepairList = matRepairMapper.queryMatRepair(matRepair1);
            for (MatRepair matRepair : matRepairList) {
                Integer matRepairId = matRepair.getMatRepairId();
                OrderRepair orderRepair = new OrderRepair();
                orderRepair.setOrderTechId(orderTechId);
                orderRepair.setRepairId(matRepair.getRepairId());
                orderRepair.setDataState(1);
                orderRepairMapper.insertOrderRepair(orderRepair);
                Integer orderRepairId = orderRepair.getOrderRepairId();

                //新增工序
                MatRepairFlow matRepairFlow1 = new MatRepairFlow();
                matRepairFlow1.setMatRepairId(matRepairId);
                List<MatRepairFlow> matRepairFlowList = matRepairFlowMapper.queryMatRepairFlow(matRepairFlow1);
                for (MatRepairFlow matRepairFlow : matRepairFlowList) {
                    //查询流程id
                    OrderFlow orderFlow = new OrderFlow();
                    orderFlow.setOrderTechId(orderTechId);
                    List<OrderFlow> orderFlowList = orderFlowMapper.queryOrderFlow(orderFlow);
                    for (OrderFlow orderFlow1 : orderFlowList) {
                        if (matRepairFlow.getProId() != null && matRepairFlow.getProId() != null) {
                            if (matRepairFlow.getProId().equals(orderFlow1.getProId()) && matRepairFlow.getSerialNum().equals(orderFlow1.getSerialNum())) {
                                OrderRepairFlow orderRepairFlow = new OrderRepairFlow();
                                orderRepairFlow.setOrderFlowId(orderFlow1.getOrderFlowId());
                                orderRepairFlow.setOrderRepairId(orderRepairId);
                                orderRepairFlow.setOrderRepairFlowId(null);
                                orderRepairFlow.setDataState(1);
                                orderRepairFlowMapper.insertOrderRepairFlow(orderRepairFlow);

                            }

                        }
                    }


                }

                //新增采集项目
                RepairDetail repairDetail = new RepairDetail();
                repairDetail.setRepairId(matRepair1.getRepairId());
                List<RepairDetail> repairDetailList = repairDetailMapper.selectAll(repairDetail);
                for (RepairDetail repairDetail1 : repairDetailList) {
                    MatRepairDete matRepairDete = new MatRepairDete();
                    matRepairDete.setMatRepairId(matRepairId);
                    matRepairDete.setRepairDetailId(repairDetail1.getRepairDetailId());
                    List<MatRepairDete> matRepairDeteList = matRepairDeteMapper.queryMatRepairDete(matRepairDete);
                    for (MatRepairDete matRepairDete1 : matRepairDeteList) {
                        OrderRepairDete orderRepairDete = new OrderRepairDete();
                        orderRepairDete.setOrderRepairId(orderRepairId);
                        orderRepairDete.setRepairDetailId(matRepairDete1.getRepairDetailId());
                        orderRepairDete.setLowerLimit(matRepairDete1.getLowerLimit());
                        orderRepairDete.setMiddleLimit(matRepairDete1.getMiddleLimit());
                        orderRepairDete.setUpperLimit(matRepairDete1.getUpperLimit());
                        orderRepairDete.setDeteId(matRepairDete1.getDeteId());
                        orderRepairDete.setDataState(1);
                        orderRepairDete.setOrderRepairDeteId(null);
                        orderRepairDete.setShowDete(matRepairDete1.getShowDete());
                        orderRepairDete.setSampleNum(matRepairDete1.getSampleNum());
                        orderRepairDete.setSerialNum(matRepairDete1.getSerialNum());
                        orderRepairDete.setSampleCount(matRepairDete1.getSampleCount());
                        orderRepairDeteMapper.insertOrderRepairDete(orderRepairDete);
                    }

                }


            }

            Order order1=new Order();
            order1.setOrderId(orderId);
            order1.setOrderTechId(orderTechId);
            orderMapper.updateByPrimaryKeySelective(order1);


            return JSONObject.oK(HintMessage.SAVE_SUCCESS, orderTechId);

        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 任务单流程维修下工序新增查询
     */
    @Override
    public JSONObject queryListOrderFlowToInsert(OrderFlow orderFlow) {
        try {
            //获取页面的数据个数
            Integer pagesize = orderFlow.getPagesize();
            //获取查询的页数
            Integer pageindex = orderFlow.getPageindex();
            if (pagesize != null && pageindex != null) {
                orderFlow.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<OrderFlow> orderFlowList = orderFlowMapper.queryOrderFlowToInsert(orderFlow);

            //如果页数和页面数量都不为空的情况下进行计数
            if (orderFlowList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, orderFlowList, 0);
            } else {
                Integer count = orderFlowMapper.countOrderFlowToInsert(orderFlow);
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, orderFlowList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    public Integer copyTech(Integer oldOrderTechId,Integer sourceTypeId ){
        //复制新流程
        //查询旧版本


        OrderTech oldOrderTech=new OrderTech();
        oldOrderTech.setOrderTechId(oldOrderTechId);
        //查询原版本
        List<OrderTech> orderTeches = orderTechMapper.queryOrderTech(oldOrderTech);
        if(orderTeches.size()!=0){
            oldOrderTech=orderTeches.get(0);
        }
        //创建新版本

        OrderTech newOrderTech=new OrderTech();
        BeanUtils.copyProperties(oldOrderTech,newOrderTech);
        newOrderTech.setOrderTechId(null);
        newOrderTech.setSourceTypeId(sourceTypeId);

        orderTechMapper.insertOrderTech(newOrderTech);
        Integer  newOrderTechId=newOrderTech.getOrderTechId();
        //新增完成

        //复制流程
        OrderFlow orderFlow1=new OrderFlow();
        orderFlow1.setOrderTechId(oldOrderTechId);
        List<OrderFlow> orderFlowList = orderFlowMapper.queryOrderFlow(orderFlow1);

        List<OrderStation> orderStationInsertList=new ArrayList<>();
        List<OrderDete> orderDeteInsertList=new ArrayList<>();
        List<OrderBom> orderBomInsertList=new ArrayList<>();
        List<OrderBadpheno> orderBadphenoInsertList=new ArrayList<>();
        List<OrderRepairFlow> orderRepairFlowInsertList=new ArrayList<>();
        List<OrderRepairDete> orderRepairDeteInsertList=new ArrayList<>();

        for (OrderFlow oldOrderFlow:orderFlowList ) {

            OrderFlow orderFlowInsert=new OrderFlow();

            Integer oldOrderFlowId=oldOrderFlow.getOrderFlowId();

            OrderFlow newOrderFlow=new OrderFlow();
            BeanUtils.copyProperties(oldOrderFlow,newOrderFlow);
            newOrderFlow.setOrderTechId(newOrderTechId);
            newOrderFlow.setOrderFlowId(null);
            //新增流程
            orderFlowMapper.insertOrderFlow(newOrderFlow);
            Integer newOrderFlowId=newOrderFlow.getOrderFlowId();

            //复制工位

            List<OrderStation> orderStations = orderStationMapper.queryOrderStationByOrderFlowId(oldOrderFlowId);
            for (OrderStation orderStation:orderStations ) {
                orderStation.setOrderFlowId(newOrderFlowId);
                orderStation.setOrderStationId(null);
                orderStationInsertList.add(orderStation);
//                orderStationMapper.insertOrderStation(orderStation);
            }

            List<OrderDete> orderDetes = orderDeteMapper.queryOrderDeteByOrderFlowId(oldOrderFlowId);
            for (OrderDete orderDete:orderDetes ) {
                orderDete.setOrderFlowId(newOrderFlowId);
                orderDete.setOrderDeteId(null);
                orderDeteInsertList.add(orderDete);
//                orderDeteMapper.insertOrderDete(orderDete);
            }

            List<OrderBom> orderBoms = orderBomMapper.queryOrderBomByOrderFlowId(oldOrderFlowId);
            for (OrderBom orderBom:orderBoms ) {
                orderBom.setOrderFlowId(newOrderFlowId);
                orderBom.setOrderBomId(null);
                orderBomInsertList.add(orderBom);
//                orderBomMapper.insertOrderBom(orderBom);
            }

            List<OrderBadpheno> orderBadphenos = orderBadphenoMapper.queryOrderBadphenoByOrderFlowId(oldOrderFlowId);
            for (OrderBadpheno orderBadpheno:orderBadphenos ) {
                orderBadpheno.setOrderFlowId(newOrderFlowId);
                orderBadpheno.setOrderBadphenoId(null);
                orderBadphenoInsertList.add(orderBadpheno);
//                orderBadphenoMapper.insertOrderBadpheno(orderBadpheno);
            }

        }

        OrderRepair orderRepair1=new OrderRepair();
        orderRepair1.setOrderTechId(oldOrderTechId);
        List<OrderRepair> orderRepairList = orderRepairMapper.queryOrderRepair(orderRepair1);

        for (OrderRepair oldOrderRepair:orderRepairList ) {
            Integer oldOrderRepairId= oldOrderRepair.getOrderRepairId();
            OrderRepair newOrderRepair=new OrderRepair();
            BeanUtils.copyProperties(oldOrderRepair,newOrderRepair);
            newOrderRepair.setOrderRepairId(null);
            newOrderRepair.setOrderTechId(newOrderTechId);
            orderRepairMapper.insertOrderRepair(newOrderRepair);
            Integer newOrderRepairId=newOrderRepair.getOrderRepairId();

            List<OrderRepairFlow> orderRepairFlows = orderRepairFlowMapper.queryOrderRepairFlowByOrderRepairId(oldOrderRepairId);
            for (OrderRepairFlow oldOrderRepairFlow:orderRepairFlows ) {

                //查询工艺流程
                OrderFlow orderFlow = new OrderFlow();
                orderFlow.setOrderTechId(newOrderTechId);
                List<OrderFlow> orderFlowList1 = orderFlowMapper.queryOrderFlow(orderFlow);
                for (OrderFlow orderFlow2:orderFlowList1) {
                    if(oldOrderRepairFlow.getProId()!=null&&orderFlow2.getProId()!=null){
                        if(oldOrderRepairFlow.getProId().equals(orderFlow2.getProId())&&oldOrderRepairFlow.getSerialNum().equals(orderFlow2.getSerialNum())){
                            OrderRepairFlow newOrderRepairFlow=new OrderRepairFlow();
                            newOrderRepairFlow.setDataState(1);
                            newOrderRepairFlow.setOrderRepairFlowId(null);
                            newOrderRepairFlow.setOrderRepairId(newOrderRepairId);
                            newOrderRepairFlow.setOrderFlowId(orderFlow2.getOrderFlowId());
                            orderRepairFlowInsertList.add(newOrderRepairFlow);
//                            orderRepairFlowMapper.insertOrderRepairFlow(newOrderRepairFlow);


                        }
                    }



                }


            }

            RepairDetail repairDetail=new RepairDetail();
            repairDetail.setRepairId(oldOrderRepair.getRepairId());
            List<RepairDetail> repairDetailList = repairDetailMapper.selectAll(repairDetail);
            for (RepairDetail repairDetail1 : repairDetailList) {
                OrderRepairDete orderRepairDete1=new OrderRepairDete();
                orderRepairDete1.setRepairDetailId(repairDetail1.getRepairDetailId());
                orderRepairDete1.setOrderRepairId(oldOrderRepair.getOrderRepairId());
                List<OrderRepairDete> orderRepairDeteList = orderRepairDeteMapper.queryOrderRepairDete(orderRepairDete1);


                for (OrderRepairDete oldOrderRepairDete : orderRepairDeteList) {

                    OrderRepairDete newOrderRepairDete = new OrderRepairDete();
                    BeanUtils.copyProperties(oldOrderRepairDete,newOrderRepairDete);
                    newOrderRepairDete.setOrderRepairDeteId(null);
                    newOrderRepairDete.setOrderRepairId(newOrderRepairId);
                    orderRepairDeteInsertList.add(newOrderRepairDete);
//                    orderRepairDeteMapper.insertOrderRepairDete(newOrderRepairDete);

                }

            }


        }


        if(orderStationInsertList.size()!=0){
//            for (OrderStation list:orderStationInsertList  ) {
//                orderStationMapper.insertOrderStation(list);
//            }
            orderTechMapper.orderStationInsertList(orderStationInsertList);
        }
        if(orderDeteInsertList.size()!=0){
//            for (OrderDete list:orderDeteInsertList  ) {
//                orderDeteMapper.insertOrderDete(list);
//            }
            orderTechMapper.orderDeteInsertList(orderDeteInsertList);
        }
        if(orderBomInsertList.size()!=0){
//            for (OrderBom list:orderBomInsertList  ) {
//                orderBomMapper.insertOrderBom(list);
//            }
            orderTechMapper.orderBomInsertList(orderBomInsertList);
        }
        if(orderBadphenoInsertList.size()!=0){
//            for (OrderBadpheno list:orderBadphenoInsertList  ) {
//                orderBadphenoMapper.insertOrderBadpheno(list);
//            }
            orderTechMapper.orderBadphenoInsertList(orderBadphenoInsertList);
        }
        if(orderRepairFlowInsertList.size()!=0){
//            for (OrderRepairFlow list:orderRepairFlowInsertList  ) {
//                orderRepairFlowMapper.insertOrderRepairFlow(list);
//            }
            orderTechMapper.orderRepairFlowInsertList(orderRepairFlowInsertList);
        }
        if(orderRepairDeteInsertList.size()!=0){
//            for (OrderRepairDete list:orderRepairDeteInsertList  ) {
//                orderRepairDeteMapper.insertOrderRepairDete(list);
//            }
            orderTechMapper.orderRepairDeteInsertList(orderRepairDeteInsertList);
        }




        return  newOrderTechId;
        //复制流程完成
    }
}
